package com.zz.springrabbitmq.controller;

import com.zz.springrabbitmq.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
     * 简单模式
     */
    @RequestMapping("/simpleSend")
    public String simpleSend() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.SIMPLE_QUEUE, "simple message" + i);
        }
        System.out.println("消息发送成功");
        return "消息发送成功";
    }
    /*
     * 广播模式
     */
    @RequestMapping("/fanoutSend")
    public String fanoutSend() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "fanout message" + i);
        }
        System.out.println("消息发送成功");
        return "消息发送成功";
    }
    /*
     * 路由模式
     */
    @RequestMapping("/directSend")
    public String directSend() {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, "orange", "orange message");
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, "black", "black message");
        System.out.println("消息发送成功");
        return "消息发送成功";
    }
    /*
     * 通配符模式
     */
    @RequestMapping("/topicSend")
    public String topicSend() {
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, "a.orange.rabbit", "a.orange.rabbit");
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, "lazy", "lazy");
        System.out.println("消息发送成功");
        return "消息发送成功";
    }
}
